package com.ys.peony.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ys.peony.R;
import com.ys.peony.interfaces.ItemClickListener;
import com.ys.peony.interfaces.OnContractClickListener;
import com.ys.peony.model.domain.ContractSortModel;
import com.ys.peony.model.domain.DataBean;
import com.ys.peony.ui.holder.BaseViewHolder;
import com.ys.peony.ui.holder.ContractChildViewHolder;
import com.ys.peony.ui.holder.ContractParentViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 59153 on 2018/3/27.
 */

public class ContractSortAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private Context context;
    private List<ContractSortModel.FileTemplateBean> mList;
    private LayoutInflater mInflater;
    private final List<Integer> mGroupPositions;
    private OnContractClickListener listener;
    private int count = 0;

    public ContractSortAdapter(Context context, List<ContractSortModel.FileTemplateBean> dataBeanList, OnContractClickListener listener) {
        this.context = context;
        this.mList = dataBeanList;
        this.mInflater = LayoutInflater.from(context);
        this.listener = listener;
        mGroupPositions = new ArrayList<>();
        initGroupPositions();
    }

    public void setData(List<ContractSortModel.FileTemplateBean> list) {
        this.mList = list;
        initGroupPositions();
        notifyDataSetChanged();
    }

    public void initGroupPositions() {
        mGroupPositions.clear();
        int groupPosition = 0;
        for (int i = 0; i < mList.size(); i++) {
            if (i == 0) {
                groupPosition = 0;
            } else {
                groupPosition += (getChildCount(i - 1) + 1);
            }
            mGroupPositions.add(groupPosition);
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case DataBean.PARENT_ITEM:
                view = mInflater.inflate(R.layout.item_contract_parent, parent, false);
                return new ContractParentViewHolder(context, view);
            case DataBean.CHILD_ITEM:
                view = mInflater.inflate(R.layout.item_contract_child, parent, false);
                return new ContractChildViewHolder(context, view);
            default:
                view = mInflater.inflate(R.layout.item_contract_parent, parent, false);
                return new ContractParentViewHolder(context, view);
        }
    }

    /**
     * 根据不同的类型绑定View
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        if (holder instanceof ContractParentViewHolder) {
            ContractParentViewHolder parentViewHolder = (ContractParentViewHolder) holder;
            int pos = getGroupPosition(position);
            parentViewHolder.bindView(mList.get(pos), pos, itemClickListener);
        } else if (holder instanceof ContractChildViewHolder) {
            ContractChildViewHolder childViewHolder = (ContractChildViewHolder) holder;
            int pos1 = getGroupPosition(position);
            childViewHolder.bindView(mList.get(pos1), pos1, getChildPosition(position), listener);
        }
    }

    int getChildPosition(int position) {
        int groupPosition = getGroupPosition(position);
        int absGroupPosition = mGroupPositions.get(groupPosition);
        int childPositionInGroup = position - absGroupPosition - 1;
        return childPositionInGroup;
    }

    private int getGroupPosition(int position) {
        int groupPosition = 0;
        for (int i = mGroupPositions.size() - 1; i >= 0; i--) {
            if (position >= mGroupPositions.get(i)) {
                groupPosition = i;
                break;
            }
        }
        return groupPosition;
    }


    private int getChildCount(int groupPosition) {
        List<ContractSortModel.FileTemplateBean.BusiFileTemplateListBean> beans = mList.get(groupPosition).getBusiFileTemplateList();
        return (beans != null && mList.get(groupPosition).isExpand()) ? beans.size() : 0;
    }


    @Override
    public int getItemCount() {
        int count = 0;
        for (int i = 0; i < mList.size(); i++) {
            count += getChildCount(i);
        }
        count += mList.size();
        return count;
    }


    @Override
    public int getItemViewType(int position) {
        if (mGroupPositions.contains(position)) {
            return DataBean.PARENT_ITEM;
        } else {
            return DataBean.CHILD_ITEM;
        }
    }

    private ItemClickListener itemClickListener = new ItemClickListener() {
        @Override
        public void onExpandChildren(int position) {
            if (mList.get(position).getBusiFileTemplateList().size() < 1) {
                return;
            }

            //在当前的item下方插入
            add(position);
            /*if (position == dataBeanList.size() - 2 && mOnScrollListener != null) { //如果点击的item为最后一个
                mOnScrollListener.scrollTo(position + 1);//向下滚动，使子布局能够完全展示
            }*/
        }

        @Override
        public void onHideChildren(int position) {
            if (mList.get(position).getBusiFileTemplateList().size() < 1) {
                return;
            }
            //在当前的item下方移除
            remove(position);//删除
            /*if (mOnScrollListener != null) {
                mOnScrollListener.scrollTo(position);
            }*/
        }
    };


    /**
     * 在父布局下方插入子数据
     *
     * @param position
     */
    public void add(int position) {
        initGroupPositions();
        notifyItemRangeInserted(mGroupPositions.get(position) + 1, mList.get(position).getBusiFileTemplateList().size());

    }

    /**
     * 移除子布局数据
     *
     * @param position
     */
    protected void remove(int position) {
        initGroupPositions();
        notifyItemRangeRemoved(mGroupPositions.get(position) + 1, mList.get(position).getBusiFileTemplateList().size());
    }


}
